package com.qifan.energy.widget.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.qifan.energy.mvp.callback.IEventCallback;
import com.qifan.energy.R;

public class NormalCenter2Dialog extends DialogFragment {

    private TextView titleTv;
    private IEventCallback callback = new IEventCallback() {
        @Override
        public void onCancel() {

        }

        @Override
        public void onComfirm() {

        }
    };

    /* 先显示 */
    public NormalCenter2Dialog showNow2(@Nullable FragmentManager manager, @Nullable String tag) {
        showNow(manager, tag);
        return this;
    }

    /* 对外提供 */
    public NormalCenter2Dialog setEventCallback(IEventCallback callback) {
        this.callback = callback;
        return this;
    }

    /* 设置标题 */
    public NormalCenter2Dialog title(String text){
        titleTv.setText(text);
        return this;
    }

    /**
     * 设置主题需要在 onCreate() 方法中调用 setStyle() 方法
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);*/
        setStyle(DialogFragment.STYLE_NO_FRAME,R.style.NormalDialogStyle);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //无标题
        View view = inflater.inflate(R.layout.fragment_dialog_normal, container);
        findView(view);
        return view;
    }

    /* 设置View */
    private void findView(View view) {
        titleTv = view.findViewById(R.id.title_tv);
        TextView leftTv = view.findViewById(R.id.left_tv);
        TextView rightTv = view.findViewById(R.id.right_tv);
        leftTv.setOnClickListener(v -> {
            dismiss();
            if (callback != null) callback.onCancel();
        });
        rightTv.setOnClickListener(v -> {
            dismiss();
            if (callback != null) callback.onComfirm();
        });
    }
}
